﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using GameSchool.DAL;
using GameSchool.Models;
using GameSchool.ViewModels.Project;
using GameSchool.Models.AccountModels;

namespace GameSchool.Controllers
{
    public class ProjectController : Controller
    {
        private IProjectRepository m_rep;

        public ProjectController()
        {
            m_rep = new ProjectRepository(new gsEntities());
        }

        public ProjectController(IProjectRepository rep)
        {
            m_rep = rep;
        }

        [Authorize(Roles = "Kennari")]
        public ActionResult Create(int id)
        {
            var viewModel = new CreateViewModel();
            viewModel.course = m_rep.GetCourseById(id);

            return View(viewModel);
        }

        [HttpPost]
        [Authorize(Roles = "Kennari")]
        public ActionResult Create(Project project, int id)
        {
            project.CourseID = id;
            project.TeacherUserID = (Guid)Membership.GetUser().ProviderUserKey;
            project.GradePercentage = project.GradePercentage / 100;
            m_rep.InsertProject(project);
            m_rep.Save();

            return RedirectToAction("List", new { id });
        }

        [Authorize(Roles = "Kennari")]
        public ActionResult Edit(int id)
        {
            var viewModel = new EditViewModel();
            viewModel.project = m_rep.GetProjectById(id);
            viewModel.project.GradePercentage = viewModel.project.GradePercentage * 100;

            return View(viewModel);
        }

        [HttpPost]
        [Authorize(Roles = "Kennari")]
        public ActionResult Edit(Project project, int id)
        {
            var viewModel = new EditViewModel();

            if (ModelState.IsValid)
            {
                var projectToUpdate = m_rep.GetProjectById(id);
                m_rep.UpdateProject(projectToUpdate);
                projectToUpdate.Name = project.Name;
                projectToUpdate.Description = project.Description;
                projectToUpdate.HoursToFinish = project.HoursToFinish;
                projectToUpdate.TeacherUserID = (Guid)Membership.GetUser().ProviderUserKey;
                projectToUpdate.ProjectID = project.ProjectID;
                projectToUpdate.ProjectReturns = project.ProjectReturns;
                projectToUpdate.GradePercentage = project.GradePercentage / 100;

                m_rep.Save();

                return RedirectToAction("List", new { projectUpdated = project.Name });
            }
            else
            {
                ModelState.AddModelError("", "eitthvað fór úrskeiðis, yada yada yada");
            }

            return View(viewModel);
        }

        [Authorize(Roles = "Kennari")]
        public ActionResult Grade(int id, Guid studentUserId)
        {
            var viewmodel = new GradeViewModel();
            viewmodel.project = m_rep.GetProjectById(id);
            viewmodel.currentCourse = viewmodel.project.Course;
            viewmodel.projectReturns = m_rep.GetProjectReturnsByProjectReturnId(id);

            return View(viewmodel);
        }

        [HttpPost]
        [Authorize(Roles = "Kennari")]
        public ActionResult Grade(ProjectReturns projectReturns, int id)
        {
            var projectReturnsDb = m_rep.GetProjectReturnsByProjectReturnId(id);

            projectReturnsDb.GradeComment = projectReturns.GradeComment;
            projectReturnsDb.GradeDate = DateTime.Now;
            projectReturnsDb.GradedBy = (Guid)Membership.GetUser().ProviderUserKey;
            projectReturnsDb.Grade = projectReturns.Grade;

            m_rep.Save();

            return RedirectToAction("Result", new { id = projectReturnsDb.ProjectID });
        }

        [Authorize(Roles = "Kennari, Nemandi")]
        public ActionResult List(int id)
        {
            var viewModel = new ListViewModel();

            viewModel.currentCourse = m_rep.GetCourseById(id);

            if (User.IsInRole("Kennari"))
            {
                viewModel.projectsTeachers = m_rep.GetProjectsTeacher(id, (Guid)Membership.GetUser().ProviderUserKey);

                return View("ListTeacher", viewModel);
            }
            else if (User.IsInRole("Nemandi"))
            {
                viewModel.projectStudents = m_rep.GetProjectsStudents(id);

                return View("ListStudent", viewModel);
            }

            throw new AuthenticationException("Obbossí, did you follow the white rabbit?");
        }

        [Authorize(Roles = "Kennari")]
        public ActionResult Result(int id)
        {
            var viewModel = new ResultsViewModel();
            viewModel.ProjectReturnses = m_rep.GetProjectReturnsByProject(id);
            viewModel.currentCourse = m_rep.GetProjectById(id).Course;
            viewModel.projectsTeachers = m_rep.GetProjectsTeacher(id, (Guid)Membership.GetUser().ProviderUserKey);

            //óklárað
            return View("Result", viewModel);
        }

        [Authorize(Roles = "Nemandi")]
        public ActionResult Return(int id)
        {
            var viewModel = new ReturnViewModel();
            viewModel.project = m_rep.GetProjectById(id);

            return View(viewModel);
        }

        [HttpPost]
        [Authorize(Roles = "Nemandi")]
        public ActionResult Return(ProjectReturns projectReturns, int id)
        {
            Guid studentUserId = (Guid) Membership.GetUser().ProviderUserKey;
            var projectReturnsDb = m_rep.GetProjectReturnsByProject(id).SingleOrDefault(i => i.StudentUserID == studentUserId);
            var projectToReturn = m_rep.GetProjectById(id);

            if (projectReturnsDb == null)
            {
                projectReturns.ProjectID = projectToReturn.ProjectID;
                projectReturns.StudentUserID = studentUserId;
                projectReturns.ReturnDate = DateTime.Now;

                m_rep.InsertProjectReturns(projectReturns);
                m_rep.Save();
            }
            else
            {
                projectReturnsDb.ReturnComment = projectReturns.ReturnComment;
                projectReturnsDb.ReturnDate = DateTime.Now;
                //projectReturnsDb.ReturnedFile =
                m_rep.Save();
            }



            return RedirectToAction("List", new { id = projectToReturn.CourseID });
        }
    }
}
